
package com.study.services;

import java.util.Date;
import java.util.List;
import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.study.model.mdao.LogforcustDAO;
import com.study.model.mdao.SolutionMapper;
import com.study.model.mdao.UserMDAO;

@Service
@Transactional(rollbackOn = Exception.class)
public class SolutionSercice {
	@Autowired
	SolutionMapper mapper;
	@Autowired
	LogforcustDAO logforcustDAO;
	@Autowired
	UserMDAO udao;
	/**
	 * 带条件的分页查询
	 * 
	 * @param pageNum
	 * @param pageSize
	 * @param name
	 * @param cusid 
	 * @param selects
	 * @return
	 */
	public List<JSONObject> selectBySel(int pageNum, int pageSize, String name,String useid, Integer cusid) {
		PageHelper.startPage(pageNum, pageSize);
		String names = "%" + name + "%";
		if ("6".equals(useid)) {
			return mapper.selectByWhere(names,cusid);
		}else {
			return mapper.selectByWhereOne(names,useid,cusid);
		}
	
	}

	/**
	 * 根据id查询需求
	 * 
	 * @param id
	 * @return
	 */
	public JSONObject selectByid(String id) {
		return mapper.selectByid(Integer.parseInt(id));
	}

	/**
	 * 新增
	 * 
	 * @param theme
	 * @param privoder
	 * @param custname
	 * @param opp
	 * @param degree
	 * @param content
	 * @param date
	 * @return
	 */
	public boolean insertsolu(String theme, String opp, Date sdate, String cont, String fback, String custid,String useid) {
		String name = udao.getfindUsreid(useid).getString("use_name");
		boolean flag = mapper.insertsolu(theme, opp, sdate, cont, fback, custid);
         if (flag) {
        	 String logMessage ="员工"+name+"在"+new Date()+"新增了解决方案信息";
    		 int cus = Integer.parseInt(custid);
    			logforcustDAO.insert(cus, new Date(), Integer.parseInt(useid), 2202, logMessage, "");
		}
		return flag;
	}
    /**
     * 修改
     * @param cusid
     * @param theme
     * @param sdate
     * @param cont
     * @param fback
     * @param solid
     * @param useid
     * @return
     */
	public boolean updatedemand(String cusid,String theme,Date sdate,String cont,String fback,String solid,String useid) {
		 String name = udao.getfindUsreid(useid).getString("use_name");
		boolean flag = mapper.updatedem(theme, sdate, cont, fback,Integer.parseInt(solid));
		 String logMessage ="员工"+name+"在"+new Date()+"修改了  " +theme + "解决方案信息";
		 int cus = Integer.parseInt(cusid);
			logforcustDAO.insert(cus, new Date(), Integer.parseInt(useid), 222, logMessage, "");


		return flag;
	}

}
